﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SharePointLogBrowser2
{
    public partial class FoldersAndFiles : UserControl
    {
        public FoldersAndFiles()
        {
            InitializeComponent();

            ddlSearchPattern.SelectedIndex = 0;
        }

        public EventHandler SelectedFileChanged;
        private void DoSelectFileChanged()
        {
            if (SelectedFileChanged != null)
            {
                SelectedFileChanged(this, EventArgs.Empty);
            }
        }

        public string SelectedPath
        {
            get { return tbxFolder.Text; }
            set { tbxFolder.Text = value; }
        }

        public List<FileInfo> SelectedFiles
        {
            get
            {
                List<FileInfo> fileInfo = GetSelectedFileInfos();

                return fileInfo;
                //if (fileInfo != null)
                //{
                //    return fileInfo.FullName;
                //}
                //return null;
            }
        }


        private List<FileInfo> GetSelectedFileInfos()
        {
            List<FileInfo> list = new List<FileInfo>();
            list.AddRange(from ListViewItem lvi in lvFiles.SelectedItems select lvi.Tag as FileInfo);
            return list;
        }

        private void BtnSelectFolderClick(object sender, EventArgs e)
        {
            if (fbd.ShowDialog() == DialogResult.OK)
            {
                tbxFolder.Text = fbd.SelectedPath;
                LoadListViewWithFiles(tbxFolder.Text, ddlSearchPattern.Text);
            }
        }

        private void LoadListViewWithFiles(string path, string searchPattern)
        {
            try
            {
                if (string.IsNullOrEmpty(path))
                {
                    return;
                }

                lvFiles.BeginUpdate();
                lvFiles.Items.Clear();
                DirectoryInfo di = new DirectoryInfo(path);
                FileInfo[] fileInfos = di.GetFiles(searchPattern);
                foreach (FileInfo fileInfo in fileInfos.OrderByDescending(fi => fi.LastWriteTime))
                {
                    ListViewItem lvi = new ListViewItem(new string[]{ fileInfo.Name, 
                        fileInfo.LastWriteTime.ToShortDateString() + " " + fileInfo.LastWriteTime.ToShortTimeString() });

                    lvFiles.Items.Add(lvi);

                    lvi.Tag = fileInfo;
                }
                lvFiles.EndUpdate();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void DdlSearchPatternSelectedIndexChanged(object sender, EventArgs e)
        {
            LoadListViewWithFiles(tbxFolder.Text, ddlSearchPattern.Text);
        }

        private void LvFilesDoubleClick(object sender, EventArgs e)
        {
            DoSelectFileChanged();
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            DoSelectFileChanged();
        }

        private void FoldersAndFiles_Load(object sender, EventArgs e)
        {
            LoadListViewWithFiles(tbxFolder.Text, ddlSearchPattern.Text);
        }

        private void tbxFolder_Leave(object sender, EventArgs e)
        {
            LoadListViewWithFiles(tbxFolder.Text, ddlSearchPattern.Text);
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            LoadListViewWithFiles(tbxFolder.Text, ddlSearchPattern.Text);
        }

    }
}
